package org.example.maven.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageRowBounds;
import org.example.maven.mapper.ExhibitionMapper;
import org.example.maven.pojo.exhibition.list.AddExhibition;
import org.example.maven.pojo.exhibition.list.ExhibitionListTable;
import org.example.maven.pojo.exhibition.list.SearchExhibition;
import org.example.maven.pojo.exhibition.list.ticket.ExhibitionListTicket;
import org.example.maven.pojo.exhibition.list.ticket.ExhibitionListWeb;
import org.example.maven.pojo.exhibition.list.ticket.ExhibitionTicket;
import org.example.maven.pojo.exhibition.list.ticket.UserPaidTicket;
import org.example.maven.service.ExhibitionService;
import org.example.maven.utils.Date.IsWeekend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import static org.example.maven.common.StaticVariable.Exhibition_Ticket_Inventory;

@Service
public class ExhibitionServiceImpl implements ExhibitionService {
    @Autowired
    private ExhibitionMapper exhibitionMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public List<ExhibitionListTable> getAllExhibition(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return exhibitionMapper.getAllExhibition();
    }

    @Override
    public Integer getExhibitionCount(SearchExhibition searchExhibition) {
        return exhibitionMapper.getExhibitionCount();
    }

    @Override
    public void addExhibition(AddExhibition addExhibition) {
        exhibitionMapper.addExhibition(addExhibition);
    }

    @Override
    public void setExhibitionCategory(int exhibitionId, int[] category) {
        exhibitionMapper.setExhibitionCategory(exhibitionId,category);
    }

    @Override
    public ExhibitionListTable getExhibitionById(int id) {
        return exhibitionMapper.getExhibitionById(id);
    }

    @Override
    @Transactional
    public void setExhibitionTicket(ExhibitionListTicket exhibitionListTicket) {
        //exhibitionMapper.deleteExhibitionTicket(exhibitionListTicket.getExhibitionId());
        for(ExhibitionTicket e:exhibitionListTicket.getTickets()){
            if(exhibitionMapper.getExhibitionTicketByExhibitionIdAndDate(exhibitionListTicket.getExhibitionId(),e.getDate())>0){

                exhibitionMapper.setExhibitionTicket(exhibitionListTicket.getExhibitionId(),e);
            }else{
                exhibitionMapper.addExhibitionTicket(exhibitionListTicket.getExhibitionId(),e);
                //添加库存到redis中
                stringRedisTemplate.opsForValue().set(Exhibition_Ticket_Inventory+e.getId(), String.valueOf(e.getAvailableTickets()));
            }
        }
    }

    @Override
    public List<ExhibitionListWeb> getExhibitionTicket(int id) throws ParseException {
        List<ExhibitionListWeb> exhibitionListWebs = new ArrayList<>();
        List<ExhibitionTicket> tickets =exhibitionMapper.getExhibitionTicket(id);
        for(ExhibitionTicket ticket:tickets){
            if(ticket.getDate()!=null){
                ExhibitionListWeb exhibitionListWeb=new ExhibitionListWeb();
                exhibitionListWeb.setDate(ticket.getDate());
                exhibitionListWeb.setAvailableTickets(ticket.getAvailableTickets());
                exhibitionListWeb.setFormattedDate(ticket.getDate());
                exhibitionListWeb.setTicketPrice(ticket.getTicketPrice());
                exhibitionListWeb.setPaidNumber(ticket.getPaidNumber());
                if(IsWeekend.isWorkingDay(ticket.getDate().toString()).equals("1")){
                    exhibitionListWeb.setIsWeekend(true);
                }
                if(IsWeekend.isWorkingDay(ticket.getDate().toString()).equals("2")){
                    exhibitionListWeb.setIsHoliday(true);
                }
                exhibitionListWeb.setIsHoliday(false);
                exhibitionListWeb.setIsWeekend(false);
                exhibitionListWebs.add(exhibitionListWeb);
            }
        }
        if(exhibitionListWebs.isEmpty()) {
            return null;
        }else {
            return exhibitionListWebs;
        }
    }

    @Override
    public List<UserPaidTicket> getExhibitionOrderById(int id) {
        return exhibitionMapper.getExhibitionOrderById(id);
    }
}
